package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.MemberAddr;
import com.powernode.model.Result;
import com.powernode.service.MemberAddrService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @create: 2024/7/20
 * @author: gyhe10
 * @description: 会员收货地址管理控制层
 **/

@Api(tags = "会员收货地址接口管理")
@RestController
@RequestMapping("p/address")
public class MemberAddrController {

    @Resource
    private MemberAddrService memberAddrService;

    /**
     * 查询会员收货地址集合
     * @return
     */
    @ApiOperation("查询会员收货地址集合")
    @GetMapping("list")
    public Result<List<MemberAddr>> loadMemberAddrList() {
        // 获取当前会员的openId
        String openId = AuthUtils.getMemberOpenId();
        List<MemberAddr> list = memberAddrService.queryMemberAddrList(openId);
        return Result.success(list);
    }

    /**
     * 会员新增收货地址
     * @param memberAddr 会员收货地址对象
     * @return
     */
    @ApiOperation("会员新增收货地址")
    @PostMapping
    public Result<String> saveMemberAddr(@RequestBody MemberAddr memberAddr) {
        boolean saved = memberAddrService.saveMemberAddr(memberAddr, AuthUtils.getMemberOpenId());
        return Result.handle(saved);
    }

    /**
     * 根据收货地址id查询详情
     * @param addrId 收货地址id
     * @return
     */
    @ApiOperation("根据收货地址id查询详情")
    @GetMapping("addrInfo/{addrId}")
    public Result<MemberAddr> loadMemberAddrInfo(@PathVariable Long addrId) {
        MemberAddr memberAddr = memberAddrService.getById(addrId);
        return Result.success(memberAddr);
    }

    /**
     * 修改会员收货地址
     * @param memberAddr 会员收货地址对象
     * @return
     */
    @ApiOperation("修改会员收货地址")
    @PutMapping
    public Result<String> modifyMemberAddrInfo(@RequestBody MemberAddr memberAddr) {
        Boolean modified = memberAddrService.modifyMemberAddrInfo(memberAddr, AuthUtils.getMemberOpenId());
        return Result.handle(modified);
    }

    /**
     * 删除会员收货地址
     * @param addrId 收货地址id
     * @return
     */
    @ApiOperation("删除会员收货地址")
    @DeleteMapping("deleteAddr/{addrId}")
    public Result<String> removeMemberAddr(@PathVariable Long addrId) {
        Boolean removed = memberAddrService.removeMemberAddr(addrId, AuthUtils.getMemberOpenId());
        return Result.handle(removed);
    }

    /**
     * 会员设置默认收货地址
     * @param newAddrId 新默认收货地址id
     * @return
     */
    @ApiOperation("会员设置默认收货地址")
    @PutMapping("defaultAddr/{newAddrId}")
    public Result<String> modifyMemberDefaultAddr(@PathVariable Long newAddrId) {
        String openId = AuthUtils.getMemberOpenId();
        Boolean modified = memberAddrService.modifyMemberDefaultAddr(newAddrId, openId);
        return Result.handle(modified);
    }

    //////////////// feign 接口 ////////////////

    /**
     * 根据地址ID查询会员收货地址
     * @param addrId 地址ID
     * @return
     */
    @GetMapping("getMemberAddrById")
    public Result<MemberAddr> getMemberAddrById(@RequestParam Long addrId) {
        MemberAddr memberAddr = memberAddrService.getById(addrId);
        return Result.success(memberAddr);
    }


    /**
     * 根据会员openId查询会员默认收货地址
     * @param openId 会员openId
     * @return
     */
    @GetMapping("getMemberDefaultAddrByOpenId")
    public Result<MemberAddr> getMemberDefaultAddrByOpenId(@RequestParam String openId) {
        MemberAddr memberAddr = memberAddrService.getOne(new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
                .eq(MemberAddr::getCommonAddr, 1));
        return Result.success(memberAddr);
    }
}
